package com.itheima.member;

import com.hzheima.util.ImageUtil;
import com.itheima.ExtendsClass.Fly;
import com.itheima.FinalCanstans;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class Plane2 extends Fly {
    private int xspeed;
    private int yspeed;

    public ArrayList<Bullet> getBullets() {
        return bullets;
    }

    public Plane2() {
        img = ImageUtil.getImg("FJ2.png");
        width = img.getWidth() / 2;
        hight = img.getHeight() / 2;
        x = 700;
        y = 450;
        xspeed = 3;
        yspeed = 3;
    }

    //创建方法使得飞机向左移动
    public void moveToLeft2() {
        x -= xspeed;
        if (x < 0) {
            x = 0;
        }
    }

    // 创建方法使得飞机向右移动
    public void moveToRight2() {
        x += xspeed;
        if (x > FinalCanstans.APP_WIDTH - 150) {
            x = FinalCanstans.APP_WIDTH - 150;
        }
    }

    // 创建方法使得飞机行上移动
    public void moveToUp2() {
        y -= yspeed;
        if (y < 0) {
            y = 0;
        }
    }

    //创建方法使得飞机向下移动
    public void moveToDown2() {
        y += yspeed;
        if (y > FinalCanstans.APP_HIGHT - hight - 30) {
            y = FinalCanstans.APP_HIGHT - hight - 30;
        }
    }

    int count = 0;
    //创建一个集合用来存储发射的子弹
    ArrayList<Bullet> bullets = new ArrayList<>();

    public void shootBullet() {
        count++;
        if (count % 20 == 0) {
            //创建一个子弹的对象
            Bullet bullet = new Bullet(this.x, this.y);
            //将子弹对象存到集合之中
            bullets.add(bullet);
            // 将计数器的数值重新归零
            count = 0;
            //播放子弹发射音效
            MusicUtil.playMp3("shoot.mp3");
        }
        //遍历集合取出获得集合中的每一个元素
        for (int i = 0; i < bullets.size(); i++) {
            Bullet bullet = bullets.get(i);
            //调用子弹中创建的移动方法,使得集合中的每一个元素都能移动
            bullet.move();
            //判断子弹是否出界
            if (bullet.getY() + bullet.getHight() < 0) {
                //调用集合中的方法删除出界的子弹
                bullets.remove(i);
            }
        }
    }


    public boolean ifCrash2(Ball ball) {
        boolean r = judge(ball);
        if(r){
            changImg();
        }
        return  r;
    }

    private void changImg() {
        img = ImageUtil.getImg("boom.png");
    }
}
